package com.infinitus.autocatlog.web;

import java.io.UnsupportedEncodingException;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.infinitus.autocatlog.domain.EventItem;
import com.infinitus.autocatlog.service.KafkaProSender;
import com.infinitus.autocatlog.service.KafkaTestSender;
import com.infinitus.autocatlog.service.util.Base64Coder;



@Controller
public class UitsApi {
 
    private Logger logger = Logger.getLogger(UitsApi.class);
 
    
    
    @RequestMapping(value="/mixpanel", method={ RequestMethod.GET, RequestMethod.POST })
    public @ResponseBody String mixpanel(String data) throws UnsupportedEncodingException {
    	System.out.println("-----------mixpanel----------");
		if(data == null || "".equals(data.trim())){
			return "data is blank";
		}
		try{
			JSONArray jsonArray = JSONObject.parseArray(Base64Coder.decodeString(data));
    		System.out.println(jsonArray);
    		String eventStr =  eventJson2Str(jsonArray);
    		System.out.println(eventStr);
			KafkaTestSender.getIntance().send(eventStr);
		}catch(Exception e){
			return "unable to parse data";
		}
    	System.out.println("-----------mixpanel end ----------");
		return "1";
    }
    

	@RequestMapping(value="/uitsapi", method={ RequestMethod.GET, RequestMethod.POST })
    public @ResponseBody String uitsapi(String data) throws UnsupportedEncodingException {
    	System.out.println("-----------uitsapi----------");
//    	json = URLDecoder.decode(json, "utf8") ;
//    	System.out.println("json....URLDecoder.decode:"+json);
//    	json = json.replaceAll("data=", "");
//    	System.out.println("json....URLDecoder.decode_remove_data:"+json);
    	if(data == null || "".equals(data.trim())){
    		return "data is blank";
    	}
    	try{
    		JSONArray jsonArray = JSONObject.parseArray(Base64Coder.decodeString(data));
    		System.out.println(jsonArray);
    		String eventStr =  eventJson2Str(jsonArray);
			KafkaProSender.getIntance().send(eventStr);
    	}catch(Exception e){
    		return "unable to parse data";
    	}
    	System.out.println("-----------uitsapi end ----------");
    	return "1";
    }
	
	
	
	private String eventJson2Str(JSONArray jsonArray) {
		JSONObject json = jsonArray.getJSONObject(0);
		EventItem eventItem = json.getObject("$set", EventItem.class);
		eventItem.setEventDateTime(json.getLong("$time"));
		return eventItem.toString();
    
	}
	public static void main(String[] args) {
		System.out.println(Base64Coder.decodeString("c3xldmVudF9zY3JlZW4sc3xldmVudF9hY3Rpb24sc3xldmVudF9yZWFzb24sc3xldmVudF9sYWJlbCxzfGV2ZW50X3ZhbHVlLHN8ZXZlbnRfZXh0cmEsc3x1c2VyX2lkLHN8c2Vzc2lvbl9pZCxzfEV2ZW50RGF0ZVRpbWUsc3xpcCxzfG5hdGlvbixzfHByb3ZpbmNlLHN8Y2l0eSxzfG9wZXJhdG9yLHN8bmV0d29yayxzfHN5c3RlbV9uYW1lLHN8c3lzdGVtX3ZlcnNpb24sc3xjbGllbnRfZGV2aWNlX2lkLHN8Y2xpZW50X2RldmljZV9icmFuZCxzfGNsaWVudF9kZXZpY2VfbW9kZWwsc3xjbGllbnRfZGV2aWNlX2FnZW50LHN8Y2xpZW50X2RldmljZV92ZXJzaW9uLHN8b3Nfc2NyZWVuLHN8cmVmZXJyZXIsc3xjYW1wYWlnbixzfHNvdXJjZSxzfG1lZGlhLHN8Y3JlYXRpdmUsc3xyZW1hcmssc3xFdmVudERhdGUsc3xFdmVudEhvdXIsc3xVc2VySUQ0TnVtLHN8WWVhck1vbnRoLHN8WWVhcldlZWssc3xDb2x1bW5OYW1lLHN8RGF5T2ZXZWVrAueZu+W9leS6i+S7tgHmiJDlip8BbnVsbAHnmbvlvZUBMTM3MzQ0NTg1MjYBATAzMTAwMDIyMAE3MGQ3MDI3NC05NDUxLTExZTYtYWU5My0wMDUwNTY5YTE3MTgBMjAxNy0wMi0yNyAxNjozMzowOAExMTcuMTQ4LjEyOC4yNgHkuK3lm70B5rWZ5rGfAeWugeazogHnp7vliqgBd2lmaQHml6DpmZDmnoHkuK3lm71BUFABMS4xLjEBbnVsbAFudWxsAW51bGwBbnVsbAFudWxsAW51bGwBbnVsbAFudWxsAW51bGwBbnVsbAFudWxsAQEyMDE3LTAyLTI3ATE2AW51bGwBMjAxNy0wMgFudWxsAW51bGwBTW9u"));
		/*String str = "[{\"$time\":1487918884354,\"$distinct_id\":\"unique_id\",\"$set\":{\"event_action\":\"成功\",\"event_value\":\"042009892\",\"session_id\":\"\",\"remark\":\"微信菜单-乐善会\",\"event_extra\":\"\",\"event_screen\":\"乐善会入口事件\",\"event_reson\":\"\",\"network\":\"\",\"operator\":\"\",\"nation\":\"\",\"city\":\"\",\"function\":\"乐善会\",\"ip\":\"172.20.70.155\",\"system_name\":\"无限极服务开发号\",\"province\":\"\",\"user_id\":\"042009892\",\"event_label\":\"乐善会\",\"event_datetime\":\"Fri Feb 24 14:48:04 CST 2017\",\"system_version\":\"1.1.3\"},\"$token\":\"access_token\"}]";
		JSONArray jsonArray = JSONObject.parseArray(str);
		JSONObject json = jsonArray.getJSONObject(0);
		EventItem eventItem = json.getObject("$set", EventItem.class);
		eventItem.setEventDateTime(json.getLong("$time"));
		System.out.println(eventItem);*/
	}
}
